package com.yanqu.road.server.cmd.newmonopolymkt;

import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_GET_RANK_REWARD, desc = "领取榜单奖励")
public class NewMonopolyMarketGetRankRewardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        NewMonopolyMarketProto.NewMonopolyMarketGetRankRewardReqMsg msg = NewMonopolyMarketProto.NewMonopolyMarketGetRankRewardReqMsg.parseFrom(packet.getBytes());
        if(msg.getType() != 0 && msg.getType() != 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        NewMonopolyMarketUserData userData  = player.getModule(NewMonopolyMarketModule.class).userData;
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(msg.getType() == 1){
            if(userData.isGetServerReward()){
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_REWARD_HAD_GET;
                return;
            }
        }
        if(msg.getType() == 0){
            if(userData.isGetUserReward()){
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_REWARD_HAD_GET;
                return;
            }
        }
        NewMonopolyMarketMgr.sendAppointActivityPacket(player.getUserId(), Protocol.C_NEW_MONOPOLY_MARKET_GET_RANK_REWARD, msg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_NEW_MONOPOLY_MARKET_GET_RANK_REWARD, NewMonopolyMarketProto.NewMonopolyMarketGetRankRewardRespMsg.newBuilder().setRet(ret));
        }
    }
}
